package com.gucheng.ggbi.bizmq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import lombok.extern.slf4j.Slf4j;

@Configuration
@Slf4j
public class DeadLetterQueueConfig {

    // 死信队列名称
    public static final String BI_DEAD_LETTER_QUEUE_NAME = "bi_dead_letter_queue";

    // 死信交换器名称
    public static final String BI_DEAD_LETTER_EXCHANGE_NAME = "bi_dead_letter_exchange";

    @Bean
    public Queue biDeadLetterQueue() {
        return new Queue(BI_DEAD_LETTER_QUEUE_NAME);
    }

    @Bean
    public DirectExchange biDeadLetterExchange() {
        return new DirectExchange(BI_DEAD_LETTER_EXCHANGE_NAME);
    }

    @Bean
    public Binding biDeadLetterBinding() {
        try {
            return BindingBuilder.bind(biDeadLetterQueue()).to(biDeadLetterExchange()).with("bi_dead_routing_key");
        } catch (Exception e) {
            log.error("Error creating binding for dead letter queue", e);
            return null;
        }
    }
}